package ro.prjmanager.helpers;

import ro.prjmanager.dao.ColumnsDao;
import ro.prjmanager.dao.ModelsDao;
import ro.prjmanager.database.DataBaseReader;
import ro.prjmanager.database.model.Column;
import ro.prjmanager.database.model.Table;
import ro.prjmanager.model.Columns;
import ro.prjmanager.model.Models;

import java.util.ArrayList;
import java.util.List;

public class ModelsTablesHelper {

    public static synchronized Models mergeIntoExistent(ModelsDao modelsDao,
                                                        ColumnsDao columnsDao,
                                                        Models existentModel,
                                                        Table table){
        if(existentModel==null)
            return cast(table);
        existentModel.setDatabaseName(table.getDatabaseName());
        existentModel.setDatabasePk(table.getDatabasePk());
        existentModel.setIsExtend(table.isExtend()?1:0);
        existentModel.setJavaName(table.getJavaName());
        existentModel.setPkClass(table.getPkClass());
        existentModel.setClassExtend(table.getClassExtend());

        List<Column> colsOrig=table.getColumns();
        List<Columns> colsDest=new ArrayList<Columns>();
        for(Column col:colsOrig){
            Columns col2=ColumnsColumnHelper.cast(col);
            if(col.getForeingColumn()!=null){
                Table fkTable=col.getForeingColumn().getOwnerTable();
                if(fkTable!=null){
                    String fkTableName=fkTable.getDatabaseName();
                    if(fkTableName!=null){
                        Models mod=modelsDao.findByDataBaseName(fkTableName);
                        if(mod!=null&&mod.getId()!=null&&col2.getForeignColumn()!=null&&col2.getForeignColumn().getOwnerTable()==null){
                             List<Columns> colz=columnsDao.findByHSQL("select c from "+Columns.class.getName()+" as c where c.databaseColumnName='"+
                                    col2.getForeignColumn().getDatabaseColumnName()+"' and c.ownerTable.id="+mod.getId());

                            if(mod.getColumns()==null)
                                    mod.setColumns(new ArrayList<Columns>());

                            if(colz==null||colz.isEmpty()){
                                col2.getForeignColumn().setOwnerTable(mod);
                                 mod.getColumns().add(col2.getForeignColumn());                                
                            }else{
                                Columns co=colz.get(0);
                                Columns co1=col2.getForeignColumn();
                                if(co.getColSize()==null)
                                    co.setColSize(co1.getColSize());
                                if(co.getDatabaseColumnName()==null)
                                    co.setDatabaseColumnName(co1.getDatabaseColumnName());
                                if(co.getDatabaseType()==null)
                                    co.setDatabaseType(co1.getDatabaseType());
                                if(co.getFkForeignModel()==null)
                                    co.setFkForeignModel(co1.getFkForeignModel());
                                if(co.getForeignColumn()==null)
                                    co.setForeignColumn(co1.getForeignColumn());
                                if(co.getForeignKey()==null)
                                    co.setForeignKey(co1.getForeignKey());
                                if(co.getJavaColumnName()==null)
                                    co.setJavaColumnName(co1.getJavaColumnName());
                                if(co.getJavaType()==null)
                                    co.setJavaType(co1.getJavaType());
                                if(co.getNullable()==null)
                                    co.setNullable(co1.getNullable());
                                if(co.getOrderCol()==null)
                                    co.setOrderCol(co1.getOrderCol());
                                if(co.getPrimaryKey()==null)
                                    co.setPrimaryKey(co1.getPrimaryKey());
                                if(co.getRelationColumns()==null)
                                    co.setRelationColumns(co1.getRelationColumns());
                                columnsDao.update(co);

                            }

                        }
                    }
                }
            }
            List<Columns> cols=columnsDao.findByHSQL("select c from "+Columns.class.getName()+" as c where c.databaseColumnName='"+
                    col2.getDatabaseColumnName()+"' and c.ownerTable.id="+existentModel.getId());

            if(cols==null||cols.isEmpty()){

                col2.setOwnerTable(existentModel);
                colsDest.add(col2);
                if(existentModel.getColumns()==null)
                    existentModel.setColumns(new ArrayList<Columns>());
                existentModel.getColumns().add(col2);
            }else{
                Columns co=cols.get(0);
                 if(co.getColSize()==null)
                    co.setColSize(col2.getColSize());
                if(co.getDatabaseColumnName()==null)
                    co.setDatabaseColumnName(col2.getDatabaseColumnName());
                if(co.getDatabaseType()==null)
                    co.setDatabaseType(col2.getDatabaseType());
                if(co.getFkForeignModel()==null)
                    co.setFkForeignModel(col2.getFkForeignModel());
                if(co.getForeignColumn()==null)
                    co.setForeignColumn(col2.getForeignColumn());
                if(co.getForeignKey()==null)
                    co.setForeignKey(col2.getForeignKey());
                if(co.getJavaColumnName()==null)
                    co.setJavaColumnName(col2.getJavaColumnName());
                if(co.getJavaType()==null)
                    co.setJavaType(col2.getJavaType());
                if(co.getNullable()==null)
                    co.setNullable(col2.getNullable());
                if(co.getOrderCol()==null)
                    co.setOrderCol(col2.getOrderCol());
                if(co.getPrimaryKey()==null)
                    co.setPrimaryKey(col2.getPrimaryKey());
                if(co.getRelationColumns()==null)
                    co.setRelationColumns(col2.getRelationColumns());
                columnsDao.update(co);
            }
        }

        return existentModel;

    }

    public static synchronized Models cast(Table table){
        Models models=new Models();
        models.setDatabaseName(table.getDatabaseName());
        models.setDatabasePk(table.getDatabasePk());
        models.setIsExtend(table.isExtend()?1:0);
        models.setJavaName(table.getJavaName());
        models.setPkClass(table.getPkClass());
        models.setClassExtend(table.getClassExtend());

        List<Column> colsOrig=table.getColumns();
        List<Columns> colsDest=new ArrayList<Columns>();
        for(Column col:colsOrig){
            Columns col2=ColumnsColumnHelper.cast(col);
            col2.setOwnerTable(models);
            colsDest.add(col2);
        }
        models.setColumns(colsDest);
        return models;

    }

    public static synchronized Table cast(Models table,ModelsDao modelsDao){
        Table models=new Table();
        models.setDatabaseName(table.getDatabaseName());
        models.setDatabasePk(table.getDatabasePk());
        models.setExtend(table.getIsExtend()!=null&&table.getIsExtend()==1);
        models.setJavaName(table.getJavaName());
        models.setPkClass(table.getPkClass());
        models.setClassExtend(table.getClassExtend());

        List<Columns> colsOrig=table.getColumns();
        List<Column> colsDest=new ArrayList<Column>();
        for(Columns col:colsOrig){
            Column col2=ColumnsColumnHelper.cast(col);
            List<Column> relationals=col2.getRelationColumns();
            List<Column> filteredColumn=new ArrayList<Column>();
            if(relationals!=null&&!relationals.isEmpty()){
                for(Column colR:relationals){
                    Models model=modelsDao.findByDataBaseName(colR.getOwnerTable().getDatabaseName());
                    if(model!=null&&model.getId()!=null)
                        filteredColumn.add(colR);
                }
                col2.setRelationColumns(filteredColumn);
            }


            col2.setOwnerTable(models);
            colsDest.add(col2);
        }


        models.setColumns(colsDest);
        return models;

    }




    public static void main(String[] args) {
        DataBaseReader dba= DataBaseReader.getInstance();
        Table table=dba.loadTable("MODELS",false);
        Models models=cast(table);
        System.out.println(models);
    }
}
